<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:pe="http://primefaces.org/ui/extensions">

    <h:body>

        <ui:composition template="/resources/template/template.xhtml">

            <ui:define name="content">
                <h:form>

                    <p:panel header="All Sale Bill" >

                        <h:panelGrid columns="2" >
                            <h:outputLabel value="Institution" ></h:outputLabel>
                            <p:autoComplete converter="institutionConverter"
                                            completeMethod="#{institutionController.completeIns}" var="ins" 
                                            itemLabel="#{ins.name}" itemValue="#{ins}" forceSelection="true" 
                                            value="#{billController.institution}">
                            </p:autoComplete>
                            <h:outputLabel value="Department" ></h:outputLabel>
                            <p:autoComplete completeMethod="#{departmentController.completeDept}" var="dept" 
                                            itemLabel="#{dept.name}" itemValue="#{dept}" forceSelection="true" 
                                            value="#{billController.department}" converter="departmentConverter" >
                            </p:autoComplete>
                            <h:outputLabel value="From" ></h:outputLabel>
                            <p:calendar value="#{billController.fromDate}" pattern="dd/MMM/yyyy - hh:mm:ss a" ></p:calendar>
                            <h:outputLabel value="To" ></h:outputLabel>
                            <p:calendar value="#{billController.toDate}" pattern="dd/MMM/yyyy - hh:mm:ss a" ></p:calendar>

                            <h:outputLabel value="From" ></h:outputLabel>
                            <p:selectOneMenu value="#{billController.paymentMethod}" >
                                <f:selectItem itemLabel="All" ></f:selectItem>
                                <f:selectItems value="#{enumController.paymentMethods}"  />
                            </p:selectOneMenu>

                            <h:outputLabel value="Bill Type" ></h:outputLabel>
                            <p:selectOneMenu value="#{billController.billType}" >
                                <f:selectItem itemLabel="Retail + Wholesale" ></f:selectItem>
                                <f:selectItems value="#{enumController.pharmacySaleBillTypes}"  />
                            </p:selectOneMenu>
                        </h:panelGrid>

                        <h:panelGrid columns="3">
                            <p:commandButton ajax="false" value="Fill" action="#{billController.getPharmacySaleBills}" ></p:commandButton>

                            <p:commandButton value="Print" ajax="false" action="#" >
                                <p:printer target="gpBillPreview" ></p:printer>
                            </p:commandButton>

                            <p:commandButton ajax="false" value="Excel" styleClass="noPrintButton" style="float: right;" >
                                <pe:exporter type="xlsx" target="tbl" fileName="opd_bills"  />
                            </p:commandButton>  
                        </h:panelGrid>


                        <h:panelGroup id="gpBillPreview">

                            <p:dataTable id="tbl" styleClass="noBorder normalFont"
                                         value="#{billController.bills}" var="bill"
                                         rows="20" rowKey="#{bill.id}"
                                         paginator="true"
                                         paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                                         rowsPerPageTemplate="20, 50, 100, 100000" 
                                         filteredValue="#{billController.selectedBills}"
                                         lazy="false">

                                <f:facet name="header">
                                    <h:outputLabel id="lblPharmacyBillList" value="Pharmacy Bill List"/>
                                    <br/>
                                    <h:outputLabel id="lblDept" 
                                                   rendered="#{billController.department ne null}" value="#{billController.department.name}"/> 
                                    <br/>
                                    <h:panelGroup rendered="#{commonController.sameDate(billController.fromDate, billController.toDate)}" >
                                        <h:outputLabel value="Date : "/>
                                        <h:outputLabel value="#{billController.fromDate}">
                                            <f:convertDateTime pattern="dd MM yyyy" ></f:convertDateTime>
                                        </h:outputLabel>
                                    </h:panelGroup>
                                    <h:panelGroup rendered="#{commonController.sameDate(billController.fromDate, billController.toDate)}" >
                                        <h:outputLabel value="From : "/>
                                        <h:outputLabel value="#{billController.fromDate}">
                                            <f:convertDateTime pattern="dd MM yyyy" ></f:convertDateTime>
                                        </h:outputLabel>
                                        <p:spacer width="30" height="1"/>
                                        <h:outputLabel value="To : "/>
                                        <h:outputLabel value="#{billController.toDate}">
                                            <f:convertDateTime pattern="dd MM yyyy" ></f:convertDateTime>
                                        </h:outputLabel>
                                    </h:panelGroup>
                                </f:facet>

                                <p:column headerText="Date"  
                                          sortBy="#{bill.createdAt}"
                                          rendered="#{!commonController.sameDate(billController.fromDate , billController.toDate)}">                                
                                    <h:outputLabel value="#{bill.createdAt}" >
                                        <f:convertDateTime pattern="dd MMM yy" ></f:convertDateTime>
                                    </h:outputLabel>
                                </p:column>

                                <p:column headerText="Time"  
                                          sortBy="#{bill.createdAt}"
                                          >                                
                                    <h:outputLabel value="#{bill.createdAt}" >
                                        <f:convertDateTime pattern="hh:mm a" ></f:convertDateTime>
                                    </h:outputLabel>
                                </p:column>

                                <p:column headerText="Bill No" 
                                          sortBy="#{bill.insId}"
                                          filterBy="#{bill.insId}"
                                          filterMatchMode="contains">                                
                                    <h:outputLabel   value="#{bill.insId}"/>
                                </p:column>  

                                <p:column headerText="Institution"  rendered="#{billController.institution eq null and billController.department eq null}"
                                          sortBy="#{bill.institution.name}">                                
                                    <h:outputLabel value="#{bill.institution.name}" />
                                </p:column>
                                <p:column headerText="Department"   rendered="#{billController.department eq null}"
                                          sortBy="#{bill.department.name}">                                
                                    <h:outputLabel value="#{bill.department.name}" />
                                </p:column>
                                <p:column headerText="Bill Type" 
                                          sortBy="#{bill.billType}"
                                          filterBy="#{bill.billType}"
                                          filterMatchMode="contains"
                                          >                                
                                    <h:outputLabel   value="#{bill.billType}"/>
                                </p:column> 
                                <p:column headerText="Bill Type" 
                                          sortBy="#{bill.deptId}"
                                          filterBy="#{bill.deptId}"
                                          filterMatchMode="contains"
                                          >                                
                                    <h:outputLabel   value="#{bill.deptId}"/>
                                </p:column> 
                                <p:column headerText="Customer"  
                                          sortBy="#{bill.toDepartment.name}"
                                          filterBy="#{bill.toDepartment.name}"
                                          filterMatchMode="contains">                                
                                    <h:outputLabel value="#{bill.toInstitution.name} - #{bill.toDepartment.name}" />
                                </p:column>
                                <p:column headerText="Patient"
                                          sortBy="#{bill.patient.person.nameWithTitle}"
                                          filterBy="#{bill.patient.person.nameWithTitle}"
                                          filterMatchMode="contains">                               
                                    <h:outputLabel id="lblClient" 
                                                   value="#{bill.patient.person.nameWithTitle}"/>
                                    <p:tooltip for="lblClient" >
                                        <div style="min-width: 300px!important;"  >
                                            <h:panelGrid columns="3" >
                                                <h:outputLabel value="Name" ></h:outputLabel>
                                                <p:spacer width="30" height="1" ></p:spacer>
                                                <h:outputLabel value="#{bill.patient.person.nameWithTitle}" ></h:outputLabel>
                                                <h:outputLabel value="Phone" ></h:outputLabel>
                                                <p:spacer width="30" height="1" ></p:spacer>
                                                <h:outputLabel value="#{bill.patient.person.phone}" ></h:outputLabel>
                                                <h:outputLabel value="Address" ></h:outputLabel>
                                            </h:panelGrid>
                                        </div>
                                    </p:tooltip>
                                </p:column>

                                <p:column headerText="Billed by" 
                                          sortBy="#{bill.creater.webUserPerson.name}"
                                          filterBy="#{bill.creater.webUserPerson.name}"
                                          filterMatchMode="contains">
                                    <h:outputLabel value="#{bill.creater.webUserPerson.name}" >                                      
                                    </h:outputLabel>
                                    <br/>
                                    <h:panelGroup rendered="#{bill.cancelled}" >
                                        <h:outputLabel style="color: red;" value="Cancelled By " />
                                        <h:outputLabel style="color: red;" rendered="#{bill.cancelled}" value="#{bill.cancelledBill.creater.webUserPerson.name}" >                                       
                                        </h:outputLabel>
                                    </h:panelGroup>
                                    <h:panelGroup rendered="#{bill.refunded}" >
                                        <h:outputLabel style="color: red;" value="Refunded By " />
                                        <h:outputLabel style="color: red;" rendered="#{bill.refunded}" value="#{bill.refundedBill.creater.webUserPerson.name}" >
                                        </h:outputLabel>
                                    </h:panelGroup>
                                </p:column>    

                                <p:column headerText="Payment method" 
                                          sortBy="#{bill.paymentMethod}"
                                          filterBy="#{bill.paymentMethod}"
                                          filterMatchMode="contains" 
                                          style="width: 100px!important;">                                
                                    <h:outputLabel value="#{bill.paymentMethod}" /><br/>
                                </p:column>
                                <p:column headerText="Discount Scheme" 
                                          sortBy="#{bill.paymentScheme.name}"
                                          filterBy="#{bill.paymentScheme.name}"
                                          filterMatchMode="contains" 
                                          style="width: 100px!important;">                                
                                    <h:outputLabel value="#{bill.paymentScheme.name}" />
                                </p:column>
                                <p:column headerText="Gross Value"  
                                          sortBy="#{bill.total}" styleClass="averageNumericColumn">
                                    <h:outputLabel value="#{bill.total}" >
                                        <f:convertNumber pattern="#,##0.00"/>
                                    </h:outputLabel>
                                </p:column>
                                <p:column headerText="Discount" 
                                          sortBy="#{bill.discount}" styleClass="averageNumericColumn">
                                    <h:outputLabel value="#{bill.discount}" >
                                        <f:convertNumber pattern="#,##0.00"/>
                                    </h:outputLabel>

                                </p:column>
                                <p:column headerText="Net Value" 
                                          sortBy="#{bill.netTotal}" styleClass="averageNumericColumn">
                                    <h:outputLabel value="#{bill.netTotal}" >
                                        <f:convertNumber pattern="#,##0.00"/>
                                    </h:outputLabel>
                                </p:column>  

                                <p:column headerText="Cancellations/Refunds"  
                                          sortBy="#{bill.createdAt}">
                                    <h:panelGroup rendered="#{bill.cancelled}" >
                                        <h:outputLabel style="color: red;" value="Cancelled at " />
                                        <h:outputLabel style="color: red;" rendered="#{bill.cancelled}" value="#{bill.cancelledBill.createdAt}" >
                                            <f:convertDateTime pattern="dd/MMM/yyyy - hh:mm:ss a"/>
                                        </h:outputLabel>
                                    </h:panelGroup>
                                    <h:panelGroup rendered="#{bill.refunded}" >
                                        <h:outputLabel style="color: red;" value="Refunded at " />
                                        <h:outputLabel style="color: red;" rendered="#{bill.refunded}" value="#{bill.refundedBill.createdAt}" >
                                            <f:convertDateTime pattern="dd/MMM/yyyy - hh:mm:ss a"/>
                                        </h:outputLabel>
                                    </h:panelGroup>
                                    <h:outputLabel rendered="#{bill.refundedBill ne null}" value="#{bill.refundedBill.comments}" >
                                    </h:outputLabel>
                                    <h:outputLabel  rendered="#{bill.cancelledBill ne null}" value="#{bill.cancelledBill.comments}" >
                                    </h:outputLabel>
                                </p:column>  

                                <p:columnGroup type="footer" >
                                    <p:row >
                                        <p:column headerText="Date" rendered="#{!commonController.sameDate(billController.fromDate , billController.toDate)}">                                
                                        </p:column>
                                        <p:column headerText="Time">                                
                                        </p:column>
                                        <p:column headerText="Bill No" >                                
                                        </p:column>  
                                        <p:column headerText="Institution" rendered="#{billController.institution eq null and billController.department eq null}"> 
                                        </p:column>
                                        <p:column headerText="Department"   rendered="#{billController.department eq null}"
                                                  sortBy="#{bill.department.name}">                                
                                        </p:column>
                                        <p:column headerText="Bill Type" >                                
                                        </p:column> 
                                        <p:column headerText="Bill Type" >                                
                                        </p:column> 
                                        <p:column headerText="Customer" >                                
                                        </p:column>
                                        <p:column headerText="Patient">                               
                                        </p:column>

                                        <p:column headerText="Billed by">
                                        </p:column>    

                                        <p:column headerText="Payment method" >                                
                                        </p:column>
                                        <p:column headerText="Discount Scheme">                                
                                        </p:column>
                                        <p:column headerText="Gross Value" footerText="#{billController.grosTotal}">
                                        </p:column>
                                        <p:column headerText="Discount" footerText="#{billController.discount}">
                                        </p:column>
                                        <p:column footerText="#{billController.netTotal}" >
                                        </p:column>  

                                        <p:column headerText="Cancellations/Refunds"  
                                                  sortBy="#{bill.createdAt}">
                                        </p:column>  
                                    </p:row>

                                </p:columnGroup>

                                <f:facet name="footer">
                                    <h:outputLabel style="float: right" value="Cachier : #{sessionController.loggedUser.webUserPerson.name} " />
                                </f:facet>
                            </p:dataTable>
                        </h:panelGroup> 

                    </p:panel>
                </h:form>
            </ui:define>
        </ui:composition>

    </h:body>
</html>
